package com.viqiang.ye.exception;

import com.viqiang.ye.api.CommonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName GlobalExceptionHandler
 * @Description 全局异常处理
 * @Author viqiang
 * @Date 2020/5/13 21:50
 * @Version 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public CommonResult handle(ApiException ex) {
        if (ex.getErrorCode() != null) {
            return CommonResult.failed(ex.getErrorCode());
        }
        return CommonResult.failed(ex.getMessage());
    }
}
